import presistentStorage from "@/utils/storage";
// import config from "@/config/index";
export const reuqestTask = {};
const storage = presistentStorage();
let timer = null;
let loginTimer = null;
export default {
  get(url, params, query, loading = true) {
    return this.request(url, params, query, "GET", loading);
  },
  post(url, params, query, loading = true) {
    return this.request(url, params, query, "POST", loading);
  },
  request(url, params, query, method, loading = true) {
    loading &&
      uni.showLoading({
        title: "加载中...",
        mask: true,
      });
    return new Promise((resolve, reject) => {
      const pages = getCurrentPages(); // 获取加载的页面
      const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
      const route = currentPage.route; // 当前页面的路由
      const Token = storage.getStorage("token");
      console.log("Token", Token);

      const queryString = Object.keys(query.params)
        .map(
          (key) =>
            `${encodeURIComponent(key)}=${encodeURIComponent(
              query.params[key]
            )}`
        )
        .join("&");

      console.log("qurey", query);
      console.log("queryString", queryString);

      // 拼接完整的 URL
      const fullUrl = `${url}?${queryString}`;
      let headers = {};
      if (Token) {
        headers = {
          "Content-Type": "application/json", //自定义请求头信息
          token: `${Token}`,
        };
        // config.headers.token = `${Token}`;
      } else {
        headers = {
          "Content-Type": "application/json", //自定义请求头信息
        };
      }
      const requestTask = uni.request({
        url: "http://kz.wyshuke.com:80" + fullUrl,
        data: params,
        timeout: 60000,
        dataType: "json",
        header: headers,
        method: method,
        success: (res) => {
          if (loading) {
            //做延迟关闭处理
            if (timer) {
              clearTimeout(timer);
              timer = null;
            }

            timer = setTimeout(() => {
              uni.hideLoading();
            }, 300);
          }
          if (res.statusCode == 200) {
            resolve(res.data);
          } else {
            // 接口异常处理
            if (res.statusCode == 401) {
              if (loginTimer) {
                clearTimeout(loginTimer);
                loginTimer = null;
              }

              if (route == "pages/usercenter/components/login/login") {
                reject(res.data);
                return;
              }
              storage.setStorage("redirectRoute", route);
              // 清空本地存储
              storage.clearStorage();
              loginTimer = setTimeout(() => {
                uni.reLaunch({
                  url: "/pages/usercenter/components/login/login",
                });
                reject(res.data);
              }, 300);
            } else if (res.statusCode == 403) {
              uni.showToast({
                title:
                  res.data && res.data.message ? res.data.message : "系统异常",
                icon: "error",
              });
              reject(res);
            } else if (res.statusCode == 521) {
              uni.showModal({
                title: "温馨提示",
                content: "系统升级中...",
              });
              reject(res);
            } else {
              if (
                route == "pages/usercenter/components/login/login" ||
                route == "pages/usercenter/components/register/register" ||
                route == "pages/usercenter/components/password/password" ||
                route == "pages/usercenter/components/completeInfo/completeInfo"
              ) {
                reject(res);
              } else {
                setTimeout(() => {
                  uni.showToast({
                    title:
                      res.data && res.data.message
                        ? res.data.message
                        : "系统异常",
                    icon: "error",
                    duration: 2000,
                  });
                }, 400);
                reject(res);
              }
            }
          }
        },
        fail: (err) => {
          loading && uni.hideLoading();
          reject(err);
        },
      });
    });
  },
};
